package com.apps.gomdiem.fragment;


import java.util.ArrayList;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;

import com.apps.gomdiem.R;
import com.apps.gomdiem.activity.CatalogChoiceMenu;
import com.apps.gomdiem.adapter.GridAdapter;
import com.apps.gomdiem.adapter.MenuAdapter;
import com.apps.gomdiem.helper.HomeAsyncTask;
import com.apps.gomdiem.model.ListTempMenu;
import com.apps.gomdiem.model.ListenerClass;
import com.apps.gomdiem.model.MainModel;
import com.apps.gomdiem.model.MenuList;
import com.apps.gomdiem.model.Store;
import com.viewpagerindicator.TitlePageIndicator;

public class MenuTabFragment extends Fragment {
	View root;
	// public static String mIdStore;
	TextView txtId;
	static int mPrice;
	static int mPoint;
	static int mCount;
	int mCheck;
	private String[] mListMenu = new String[] { "menu1", "menu2", "menu3" };
	private  ArrayList<ListTempMenu> mInforList= new ArrayList<ListTempMenu>();
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		root = inflater.inflate(R.layout.view_pager, container, false);
		Button btnAdd = (Button)root.findViewById(R.id.btnAddList);
		btnAdd.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
			Intent intent = new Intent(getActivity(),CatalogChoiceMenu.class);
			startActivity(intent);
			}
		});
		String mIdStore = MainModel.getInstant().getIdStore();
		try {
			if (mIdStore.equals("")) {
				Toast.makeText(getActivity(), "khong lay duoc id  " + mIdStore,Toast.LENGTH_SHORT).show();

			} else {

				ViewPager vp = (ViewPager) root.findViewById(R.id.pager);
				MenuAdapter adapter = new MenuAdapter(getChildFragmentManager(), mListMenu , onCallBack); 
				vp.setAdapter(adapter);
				TitlePageIndicator indicator = (TitlePageIndicator) root.findViewById(R.id.indicator);
				indicator.setViewPager(vp);
				TextView showGia = (TextView) root.findViewById(R.id.txtShowGia);
				TextView showPoint = (TextView) root.findViewById(R.id.txtShowDiem);
				TextView soLuong = (TextView) root.findViewById(R.id.txtShowSoLuong);
				
				for (int i = 0; i < mInforList.size(); i++) {
					mPoint+=mInforList.get(i).getPoint();
					mPrice+=mInforList.get(i).getPrice();
					mCheck =mInforList.get(i).getPosition();
				}
				showGia.setText(Integer.toString(mPrice));
				showPoint.setText(Integer.toString(mPoint));
				soLuong.setText(Integer.toString(mInforList.size()));
				
				String[] myChoice = this.getResources().getStringArray(R.array.array_list_or_grid);
				Spinner spinner = (Spinner) root.findViewById(R.id.spiner_chossen);
				
				// Create an ArrayAdapter using the string array and a default spinner
				// layout
				ArrayAdapter<CharSequence> spinerAdapter = ArrayAdapter.createFromResource(
						getActivity(), R.array.array_list_or_grid,
						android.R.layout.simple_spinner_item);

				// Specify the layout to use when the list of choices appears
				spinerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
				// Apply the adapter to the spinner
				spinner.setAdapter(spinerAdapter);
				spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

					@Override
					public void onItemSelected(AdapterView<?> arg0, View arg1,
							int arg2, long arg3) {
						// TODO Auto-generated method stub
						
					}

					@Override
					public void onNothingSelected(AdapterView<?> arg0) {
						// TODO Auto-generated method stub
						
					}
				});
			}
		} catch (Exception e) {
			// Toast.makeText(getActivity(),"Khong hien menu khi chua chon quan ",
			// Toast.LENGTH_SHORT).show();
		}
		return root;

	}

	public void onStart() {
		super.onStart();

	}
	
	private OnCheckBoxClickedListener onCallBack = new OnCheckBoxClickedListener() {
		
		@Override
		public void onUnchecked(MenuList menuList) {
			mInforList.remove(menuList.position);
		}
		
		@Override
		public void onChecked(MenuList menuList) {
			ListTempMenu pList =new ListTempMenu();
			
			pList.nameMenu = menuList.nameMenu;
			pList.price = menuList.price;
			pList.point = menuList.point;
			pList.position = menuList.position;
			
			mInforList.add(pList);
			
			
		}
	};
	
	
	//Khoahm: táº¡o call back nÃ¨ Trung
	public interface OnCheckBoxClickedListener{
		public void onChecked(MenuList menuList);
		public void onUnchecked(MenuList menuList);
	}

}
